window.onload = function () {
    document.getElementById("communication-sent").className = "add_class";
    document.getElementById("correspondence").className = "add-nav-active";
    // inbox_detail_initialize()
    RenderInboxDetail()

};


function inbox_detail_initialize(page=1,query_type='',query_value='') {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/correspondence/inbox.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                window.JSONdetail = JsonDate["data"]["user_li"];
                window.page = page;
                create_table(JsonDate["data"]["total_count"], JsonDate["data"]["inbox_li"]);
                pagenum(JsonDate["data"]["total_count"], page,query_type,query_value);
            }
        }
    };
}



// 删除列表
function deltr(){
    var tb = document.getElementById('t_table');
    var rowNum=tb.rows.length;
    for (i=0;i<rowNum;i++) {
        tb.deleteRow(i);
        rowNum=rowNum-1;
        i=i-1;
    }
}



// 全选与反全选
function check_all_not() {
    var check_box_list = document.getElementsByClassName("check_box");
    if (document.getElementById("check_all").checked) {
        for (var t=0; t<check_box_list.length; t++) {
            check_box_list[t].checked = true;
        }
    }else {
        for (var j=0; j<check_box_list.length; j++) {
            check_box_list[j].checked = false;
        }
    }
}

// 确认删除
function ConfirmDelete() {
    var myArray = new Array();
    var check_box_list = document.getElementsByClassName("check_box");
    var tb_list = document.getElementsByClassName("username");
    for (var t=0; t<check_box_list.length; t++) {
        if (check_box_list[t].checked) {
            console.log(check_box_list[t])
            console.log(tb_list[t])
            myArray.push(check_box_list[t].id)
        }
    }
    if (myArray.length === 0){
        document.getElementById("Confirm-content").innerHTML = "没有选择删除的用户！";
    }else {
        document.getElementById("Confirm-content").innerHTML = "确认要删除这"+String(myArray.length)+"个用户吗？"
    }
}

// 批量删除数据
function DeleteData() {
    var myArray = []
    var check_box_list = document.getElementsByClassName("check_box");
    var tb_list = document.getElementsByClassName("username");
    for (var t=0; t<check_box_list.length; t++) {
        if (check_box_list[t].checked) {
            myArray.push(tb_list[t].title)
        }
    }
    if (myArray.length === 0){
        document.getElementById("close-model2").click();
        return myArray
    }
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/system-setup/user-message", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("delete_list="+String(myArray)+"&page=1");
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("close-model2").click();
                initialize();
            }
        }
    };
}


function RenderInboxDetail() {
    // var myArray = document.getElementById("close-model2");
    // document.getElementById("mail_title").innerHTML='飒飒飒飒飒飒';
    // document.getElementById("mail_title").innerText='飒飒飒飒飒飒';
    console.log('qqqqqqqqqqqqqqqqqqqqqqqq')
}


function reply_message() {
    document.getElementById("loading").style.display='inline-block';
    // var form = new FormData();
    // var recipient = document.getElementById("sender").innerHTML;
    // console.log(recipient)
    // var xhr=new XMLHttpRequest();
    // var local_host = window.location.host;
    // xhr.open('get', "/correspondence/reply-message&recipient="+String(recipient), true);
    // form.append('recipient', String(recipient));
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    var recipient = document.getElementById("sender").innerHTML.replaceAll("&lt;", "").replaceAll("&gt;", "");
    console.log(recipient)
    window.location = "/correspondence/reply-message?recipient="+String(recipient);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');

    // xhr.send();
    // xhr.onreadystatechange=function(){
    //     if (xhr.readyState === 4) {
    //         if (xhr.status === 200) {
    //             document.getElementById("loading").style.display='none';
    //             // var JsonDate = JSON.parse(xhr.responseText);
    //             // if (JsonDate["code"] === 200){
    //             //     alert(JsonDate["message"]);
    //             // }
    //             // else {
    //             //     alert(JsonDate["message"])
    //             // }
    //         }
    //     }
    // }
}
